package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JxExamRecordMapper;
import com.ruoyi.system.domain.JxExamRecord;
import com.ruoyi.system.service.IJxExamRecordService;
import com.ruoyi.system.service.IJxRetakeScheduleService;
import com.ruoyi.system.service.IJxStudentProgressService;

/**
 * 学员考试记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@Service
public class JxExamRecordServiceImpl implements IJxExamRecordService 
{
    @Autowired
    private JxExamRecordMapper jxExamRecordMapper;

    @Autowired
    private IJxRetakeScheduleService jxRetakeScheduleService;

    @Autowired
    private IJxStudentProgressService jxStudentProgressService;

    /**
     * 查询学员考试记录
     * 
     * @param examId 学员考试记录主键
     * @return 学员考试记录
     */
    @Override
    public JxExamRecord selectJxExamRecordByExamId(Long examId)
    {
        return jxExamRecordMapper.selectJxExamRecordByExamId(examId);
    }

    /**
     * 查询学员考试记录列表
     * 
     * @param jxExamRecord 学员考试记录
     * @return 学员考试记录
     */
    @Override
    public List<JxExamRecord> selectJxExamRecordList(JxExamRecord jxExamRecord)
    {
        return jxExamRecordMapper.selectJxExamRecordList(jxExamRecord);
    }

    /**
     * 新增学员考试记录
     * 
     * @param jxExamRecord 学员考试记录
     * @return 结果
     */
    @Override
    public int insertJxExamRecord(JxExamRecord jxExamRecord)
    {
        jxExamRecord.setCreateTime(DateUtils.getNowDate());
        return jxExamRecordMapper.insertJxExamRecord(jxExamRecord);
    }

    /**
     * 修改学员考试记录
     * 
     * @param jxExamRecord 学员考试记录
     * @return 结果
     */
    @Override
    public int updateJxExamRecord(JxExamRecord jxExamRecord)
    {
        jxExamRecord.setUpdateTime(DateUtils.getNowDate());
        return jxExamRecordMapper.updateJxExamRecord(jxExamRecord);
    }

    /**
     * 批量删除学员考试记录
     * 
     * @param examIds 需要删除的学员考试记录主键
     * @return 结果
     */
    @Override
    public int deleteJxExamRecordByExamIds(Long[] examIds)
    {
        return jxExamRecordMapper.deleteJxExamRecordByExamIds(examIds);
    }

    /**
     * 删除学员考试记录信息
     * 
     * @param examId 学员考试记录主键
     * @return 结果
     */
    @Override
    public int deleteJxExamRecordByExamId(Long examId)
    {
        return jxExamRecordMapper.deleteJxExamRecordByExamId(examId);
    }

    /**
     * 查询学员某科目的最大考试次数
     * 
     * @param studentId 学员ID
     * @param subjectId 科目ID
     * @return 最大考试次数
     */
    @Override
    public Integer selectMaxAttemptNo(Long studentId, Long subjectId)
    {
        return jxExamRecordMapper.selectMaxAttemptNo(studentId, subjectId);
    }

    /**
     * 查询学员某科目的考试记录
     * 
     * @param studentId 学员ID
     * @param subjectId 科目ID
     * @return 考试记录列表
     */
    @Override
    public List<JxExamRecord> selectJxExamRecordByStudentAndSubject(Long studentId, Long subjectId)
    {
        return jxExamRecordMapper.selectJxExamRecordByStudentAndSubject(studentId, subjectId);
    }

    /**
     * 查询学员所有科目的考试记录
     * 
     * @param studentId 学员ID
     * @return 考试记录列表
     */
    @Override
    public List<JxExamRecord> selectJxExamRecordByStudent(Long studentId)
    {
        return jxExamRecordMapper.selectJxExamRecordByStudent(studentId);
    }

    /**
     * 登记考试记录（包含业务规则）
     * 
     * @param jxExamRecord 考试记录
     * @return 结果
     */
    @Override
    public int registerExamRecord(JxExamRecord jxExamRecord)
    {
        // 检查是否可以参加考试
        if (!canTakeExam(jxExamRecord.getStudentId(), jxExamRecord.getSubjectId())) {
            throw new RuntimeException("学员不满足考试条件");
        }

        // 自动计算考试次数
        Integer maxAttemptNo = selectMaxAttemptNo(jxExamRecord.getStudentId(), jxExamRecord.getSubjectId());
        jxExamRecord.setAttemptNo(maxAttemptNo == null ? 1 : maxAttemptNo + 1);

        // 设置创建信息
        jxExamRecord.setCreateTime(DateUtils.getNowDate());
        jxExamRecord.setCreateBy(SecurityUtils.getUsername());

        // 插入考试记录
        int result = jxExamRecordMapper.insertJxExamRecord(jxExamRecord);

        // 如果考试未通过，自动生成补考安排
        if ("未通过".equals(jxExamRecord.getResult())) {
            // 计算补考时间（考试后7天）
            Date retakeTime = new Date(jxExamRecord.getExamTime().getTime() + 7 * 24 * 60 * 60 * 1000L);
            jxRetakeScheduleService.autoGenerateRetakeSchedule(
                jxExamRecord.getStudentId(), 
                jxExamRecord.getSubjectId(), 
                retakeTime
            );
        }

        // 如果考试通过，更新学习进度状态
        if ("通过".equals(jxExamRecord.getResult())) {
            jxStudentProgressService.updateProgressStatus(
                jxExamRecord.getStudentId(), 
                jxExamRecord.getSubjectId(), 
                "完成"
            );
        }

        return result;
    }

    /**
     * 检查学员是否可以参加某科目考试
     * 
     * @param studentId 学员ID
     * @param subjectId 科目ID
     * @return 是否可以考试
     */
    @Override
    public boolean canTakeExam(Long studentId, Long subjectId)
    {
        // 检查学习进度是否满足考试条件
        // 这里可以添加更多业务规则，比如学时是否达标等
        return true; // 简化实现，实际项目中需要根据具体业务规则判断
    }
}
